package com.alibaba.citrus.cr.sales.plan.repository;


import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.PageQuerySalesPlanStrategyRequest;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.QuerySalesPlanStrategyRequest;
import com.alibaba.citrus.cr.sales.plan.sdo.SalesPlanStrategySDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.SalesPlanStrategyLoadListRequest;
import com.epoch.app.bcorder.sales_plan.salesplanstrategy.dto.CreateSalesPlanStrategyResponse;
import com.epoch.app.bcorder.sales_plan.salesplanstrategy.dto.LoadSalesPlanStrategyByOrgBusinessCodeRequest;
import com.epoch.app.bcorder.sales_plan.salesplanstrategy.dto.LoadSalesPlanStrategyRequest;
import com.epoch.app.bcorder.sales_plan.salesplanstrategy.dto.UpdateSalesPlanStrategyResponse;
import com.epoch.app.bcorder.sales_plan.salesplanstrategy.model.SalesPlanStrategy;
import com.epoch.app.bcorder.sales_plan.salesplanstrategy.service.SalesPlanStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SalesPlanStrategyRepositoryImpl implements SalesPlanStrategyRepository {

    @Resource
    private SalesPlanStrategyService salesPlanStrategyService;

    /*
     * 根据id查询策略列表
     */
    @Override
    @RepositoryInvoker
    public SalesPlanStrategySDO querySalesPlanStrategy(QuerySalesPlanStrategyRequest planQueryRequest) {
        LoadSalesPlanStrategyRequest loadSalesPlanRequest = loadSalesPlanRequestConverter(planQueryRequest);
        try {
            SalesPlanStrategy salesPlanStrategy = salesPlanStrategyService.loadSalesPlanStrategy(loadSalesPlanRequest);
            SalesPlanStrategySDO salesPlanStrategySDO = new SalesPlanStrategySDO();
            BeanUtils.copyProperties(salesPlanStrategy, salesPlanStrategySDO);
            salesPlanStrategySDO.setId(String.valueOf(salesPlanStrategy.getId()));
            return salesPlanStrategySDO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 根据业务单元编号获取计划回复策略
     */
    @Override
    @RepositoryInvoker
    public SalesPlanStrategySDO querySalesPlanStrategyByOrgBusinessCode(QuerySalesPlanStrategyRequest querySalesPlanStrategyRequest) {
        LoadSalesPlanStrategyByOrgBusinessCodeRequest  loadSalesPlanStrategyByOrgBusinessCodeRequest = loadSalesPlanStrategyConverter(querySalesPlanStrategyRequest);
        SalesPlanStrategy salesPlanStrategy = salesPlanStrategyService.loadSalesPlanStrategyByOrgBusinessCode(loadSalesPlanStrategyByOrgBusinessCodeRequest);
        SalesPlanStrategySDO salesPlanStrategySDO = new SalesPlanStrategySDO();
        BeanUtils.copyProperties(salesPlanStrategy, salesPlanStrategySDO);
        salesPlanStrategySDO.setId(String.valueOf(salesPlanStrategy.getId()));
        return salesPlanStrategySDO;
    }

    private LoadSalesPlanStrategyByOrgBusinessCodeRequest loadSalesPlanStrategyConverter(QuerySalesPlanStrategyRequest querySalesPlanStrategyRequest) {
        LoadSalesPlanStrategyByOrgBusinessCodeRequest loadSalesPlanStrategyByOrgBusinessCodeRequest = new LoadSalesPlanStrategyByOrgBusinessCodeRequest();
        BeanUtils.copyProperties(querySalesPlanStrategyRequest, loadSalesPlanStrategyByOrgBusinessCodeRequest);
        return loadSalesPlanStrategyByOrgBusinessCodeRequest;
    }

    private LoadSalesPlanStrategyRequest loadSalesPlanRequestConverter(QuerySalesPlanStrategyRequest planQueryRequest) {
        LoadSalesPlanStrategyRequest loadSalesPlanRequest = new LoadSalesPlanStrategyRequest();
        loadSalesPlanRequest.setId(Long.parseLong(planQueryRequest.getId()));
        return loadSalesPlanRequest;
    }

    /**
     * 分页查询计划策略列表
     */
    @Override
    @RepositoryInvoker
    public PageResult<List<SalesPlanStrategySDO>> pageQuery(PageQuerySalesPlanStrategyRequest pagePlanStrategyRequest) {
        SalesPlanStrategyLoadListRequest salesPlanStrategyLoadListRequest = salesPlanStrategyQueryRequestConverter(pagePlanStrategyRequest);
        try {
            Result<List<SalesPlanStrategy>> listResult = salesPlanStrategyService.loadSalesPlanStrategyList(salesPlanStrategyLoadListRequest);
            List<SalesPlanStrategySDO> collect = listResult.getResult().stream().map(SalesPlanStrategy -> {
                SalesPlanStrategySDO salesPlanStrategySDO = new SalesPlanStrategySDO();
                BeanUtils.copyProperties(SalesPlanStrategy, salesPlanStrategySDO);
                salesPlanStrategySDO.setId(String.valueOf(SalesPlanStrategy.getId()));
                return salesPlanStrategySDO;
            }).collect(Collectors.toList());
            return PageResult.of(collect, listResult.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @RepositoryInvoker
    public String save(SalesPlanStrategySDO salesPlanStrategySDO) {
        SalesPlanStrategy salesPlanStrategy = new SalesPlanStrategy();
        BeanUtils.copyProperties(salesPlanStrategySDO, salesPlanStrategy);
        salesPlanStrategy.setOrgBusinessCode(salesPlanStrategySDO.getOrgBusinessCode());
        salesPlanStrategy.setType(salesPlanStrategySDO.getType());
        salesPlanStrategy.setStatus(1);
        CreateSalesPlanStrategyResponse salesPlanStrategyResponse = salesPlanStrategyService.createSalesPlanStrategy(salesPlanStrategy);
        return salesPlanStrategyResponse.getLastInsertId();
    }

   /*
    * 修改计划回复策略状态
    */
    @Override
    @RepositoryInvoker
    public Integer editSalesPlanStrategy(SalesPlanStrategySDO salesPlanStrategySDO) {
        SalesPlanStrategy updateRequest = productPlanEditConverter(salesPlanStrategySDO);
        UpdateSalesPlanStrategyResponse UpdateSalesPlanStrategyResponse= salesPlanStrategyService.updateSalesPlanStrategy(updateRequest);
        return UpdateSalesPlanStrategyResponse.getCount();
    }

    private SalesPlanStrategy productPlanEditConverter(SalesPlanStrategySDO salesPlanStrategySDO) {
        SalesPlanStrategy updateRequest = new SalesPlanStrategy();
        BeanUtils.copyProperties(salesPlanStrategySDO, updateRequest);
        updateRequest.setId(Long.parseLong(salesPlanStrategySDO.getId()));
        return updateRequest;
    }

    private SalesPlanStrategyLoadListRequest salesPlanStrategyQueryRequestConverter(PageQuerySalesPlanStrategyRequest pageQuerySalesPlanStrategyRequest) {
        SalesPlanStrategyLoadListRequest loadSalesPlanStrategyRequest = new SalesPlanStrategyLoadListRequest();
        BeanUtils.copyProperties(pageQuerySalesPlanStrategyRequest, loadSalesPlanStrategyRequest);
        return loadSalesPlanStrategyRequest;
    }
}
